<template>
	<view>
		<view class="img" style="text-align: center; height: 220px;">
			<image src="../../static/cat-8.png" mode="" style="
			width: 250px;
			height: 250px;
			scale: 0.4;
			"></image>
		</view>


		<view class="input">

			<uni-forms ref="form" :model="user" :rules="rules">
				<uni-forms-item label="用户名" name="name">
					<uni-easyinput v-model="user.name" placeholder="请输入用户名" />


				</uni-forms-item>

				<uni-forms-item class="psw" label="密码" name="password">
					<uni-easyinput placeholder="请输入密码" type="password" v-model="user.password" />
				</uni-forms-item>

				<uni-forms-item name="">
					<button type="primary" @click="login">登录</button>
				</uni-forms-item>
			</uni-forms>

		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: '666',
					password: '666'
				},
				rules: {

					username: {
						rules: [{
							required: true,
							errorMessage: '请输入用户名',
						}]
					},
					password: {
						rules: [{
								required: true,
								errorMessage: '请输入密码',
							},
							{
								minLength: 3,
								maxLength: 5,
								errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
							}
						]
					}
				},

			}
		},
		methods: {
			login(e) {
			uni.request({
				url: 'http://localhost:8091/freshmenapp/user/login',
				method: 'post',
				data: this.user,
				success: res => {
					console.log(res);
					if(res.data.success === true){
						uni.showToast({
							title: res.data.data.name + '登录成功',
							icon:"success"
						});
						
						uni.setStorageSync('userinfo',res.data.data)
						uni.switchTab({
							url:'../my/my'
						})
						
					}else{
					uni.showToast({
						title: '用户名或密码错误',
						icon:'error'
					});
					}
				},
				fail: (err) => {
					uni.showToast({
						title: '网络错误',
						icon:'error'
					});
				},
				complete: () => {}
			});

			}
		}
	}
</script>

<style scoped>
	.input {
		width: 90%;
		margin: auto !important;
	}
</style>